package com.computergames.server.message.chessMessage;

import java.lang.reflect.InvocationTargetException;

public abstract class MultiStageGameMessage extends PerfectInformationGameMessage {
    private static String layoutInfo; // 静态布局信息

    protected MultiStageGameMessage(String chess, String[] coordinate) {
        super(chess, coordinate);
        initializeBoardFromLayout(layoutInfo); // 初始化时使用布局信息
    }

    // 静态内部类初始化布局信息并帮助创建子类实例
    public static class Builder<T extends MultiStageGameMessage> {
        private String chess;
        private String[] coordinate;
        private int halfRound;
        private int round;
        private Class<T> gameClass; // 子类类型

        public Builder(Class<T> gameClass, String chess, String[] coordinate, int halfRound, int round) {
            this.gameClass = gameClass;
            this.chess = chess;
            this.coordinate = coordinate;
            this.halfRound = halfRound;
            this.round = round;
        }

        public Builder<T> withLayoutInfo(String layoutInfo) {
            MultiStageGameMessage.layoutInfo = layoutInfo;
            return this;
        }

        public T build() throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
            // 使用反射来实例化子类
            T instance = gameClass.getDeclaredConstructor(String.class, String[].class, int.class, int.class)
                    .newInstance(chess, coordinate, halfRound, round);
            return instance;
        }
    }

    private void initializeBoardFromLayout(String layout) {
        // 解析layout字符串，初始化棋盘布局
    }

    /*
    // 创建一个具体的子类
    public class ConcreteMultiStageGame extends MultiStageGame {
    // 如果有抽象方法需要实现，在这里实现它们
    public ConcreteMultiStageGame(String chess, String[] coordinate, int halfRound, int round) {
        super(chess, coordinate, halfRound, round);
    }
    //...其他逻辑
    }

    String layout = "rnbakabnr/9/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/9/RNBAKABNR";
    try {
        MultiStageGame.ConcreteMultiStageGame game =
        new MultiStageGame.Builder<>(ConcreteMultiStageGame.class, "currentChess", new String[]{"A1", "B2"}, 1, 2)
            .withLayoutInfo(layout)
            .build();
    } catch (InstantiationException | IllegalAccessException e) {
        e.printStackTrace();
    }
     */
}
